Ext.define('js.system.RegionListShowPage', {
    extend: 'Ext.grid.Panel',
    id:'RegionListShowPage',
    requires: ['js.Util', 'js.ctrl.Common',],
    controller: 'Common',
    autoScroll : true,
    height:608,
    margin:30,
    bodyStyle: {
        backgroundColor: '#eee',
    },
    userGroupForm: function () {
        return Ext.create('js.system.RegionAddPage');
    },
    initComponent: function () {
        this.store = Ext.create('js.store.system.RegionStore');
        this.store.getProxy().setExtraParam('pageNum', 1);
        this.store.load({
            callback:function () {
                let me = Ext.getCmp('RegionListShowPage');
                if(me){
                    if(me.store.data.items.length>0){
                        new Page({
                            id: 'RegionListShowPageSupplier',
                            pageTotal: me.store.data.items[0].data.pageTotal, //必填,总页数
                            pageAmount: 10,  //每页多少条
                            dataTotal: me.store.data.items[0].data.dataTotal, //总共多少条数据
                            curPage:parseInt(me.store.data.items[0].data.pageNum), //初始页码,不填默认为1
                            pageSize: 5, //分页个数,不填默认为5
                            showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                            showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                            getPage: function (page) {
                                //获取当前页数
                                me.store.getProxy().setExtraParam('pageNum', page);
                                me.store.reload();
                            }
                        })
                    }else{
                        new Page({
                            id: 'RegionListShowPageSupplier',
                            pageTotal: 1, //必填,总页数
                            pageAmount: 10,  //每页多少条
                            dataTotal: 0, //总共多少条数据
                            pageSize: 5, //分页个数,不填默认为5
                            showPageTotalFlag:true, //是否显示数据统计,不填默认不显示
                            showSkipInputFlag:true, //是否支持跳转,不填默认不显示
                            getPage: function (page) {
                                //获取当前页数
                                me.store.getProxy().setExtraParam('pageNum', page);
                                me.store.reload();
                            }
                        })
                    }
                }
            }
        });
        this.columns = [
            {text: '城乡名称', dataIndex: 'region_name', align: 'center', flex: 1},
            {text: '城乡类型', dataIndex: 'type_name', align: 'center', flex: 1},
            {text: '是否启用', dataIndex: 'enable_char', align: 'center', flex: 1},
            {
                text: "操作",
                flex:2,
                align: 'center',
                height:44,
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {//自定义列值组合
                    return '<span style="color:#2a77d5; cursor:pointer;font-weight:bold"><a herf="#">编辑</a></span>';
                },

                sortable: true
            }
        ];
        this.dockedItems = [
            {
                dock: 'top',
                xtype: 'toolbar',
                border:'none',
                margin:'0 0 20 0',
                items: [
                    {
                        xtype : "button",
                        text:'添加',
                        cls:'button-Intelligent-sensor',
                        style:"cursor:pointer",
                        margin:'0 5% 0 0%',
                        width: '9.5%',
                        scope: this,
                        handler: this.cxqyAdd,
                        height: 32,
                    },{
                        xtype : "button",
                        text:'删除',
                        cls:'button-Intelligent-sensorA',
                        style:"cursor:pointer",
                        margin:'0 5% 0 29%',
                        width: '9.5%',
                        handler: this.projectMergerDel,
                        height: 32,
                    },{
                        xtype : "button",
                        text:'刷新',
                        cls:'button-Intelligent-sensor',
                        style:"cursor:pointer",
                        margin:'0 5% 0 29%',
                        handler: this.refresh,
                        scope: this,
                        width: '9.5%',
                        height: 32,
                    }
                ]

            },{
                xtype: 'panel',
                html:'<div id="RegionListShowPageSupplier" class="pagination"></div>',
                dock: 'bottom',
                height:50,
                margin:'-14 0 0 0',
                displayInfo: true,
            }]
        this.selModel = Ext.create('Ext.selection.CheckboxModel', {mode: 'MULTI', text: '全选'});
        this.on('cellclick', this.cellclick);
        this.callParent(arguments);
    },
    cxqyAdd:function(){
        let win = util.window(this.userGroupForm(), {title:'添加' ,width:'40%',height:'35%',bodyStyle: {backgroundColor: '#253746'}});
        win.show();
    },
    cellclick: function (td, cellIndex, record, tr, rowIndex, e, eOpts) {
        if (record === 4) {
            let formPanel = this.userGroupForm();
            let rec = this.getSelection().shift();
            formPanel.setValues(rec.data);
            let win = util.window(formPanel, {title:'修改' ,width:'40%',height:'35%'});
            win.show();
        }
    },
    //删除
    deleteManager: function () {
        let resultSelection = this.getSelection();
        let ids = Ext.Array.map(resultSelection, (item) => {
            return item.get('region_id')
        });
        util.get('/regionRoutes', {
            action_type: 'regionDel',
            region_id: ids.join(',')
        }).then(this.delSuccess, this.delFail);
    },
    projectMergerDel: function (btn) {
        let me = Ext.getCmp('RegionListShowPage');
        let userSelection = me.getSelection();
        if (userSelection.length <= 0) {
            Ext.toast('请选择需要删除的项目');
        } else {
            util.deleteConfirm(function () {
                util.prompt(me.deleteManager, me);
            })
        }
    },
    delSuccess: function (response) {
        let obj = Ext.decode(response.responseText);
        if (obj.success) {
            Ext.toast('删除成功');
            Ext.getCmp('RegionListShowPage').getStore().reload();
        } else {
            Ext.toast(obj.message);
        }
    },
    delFail: function (response) {
        Ext.toast(`删除失败：${response.status}`);
    },
    refresh: function (btn) {
        this.getStore().reload();
    }

});